/**
 * file
 */
package com.tdkj.action.front.member.buyer;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;

import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.shop.BeanGoodAuction;
import com.tdkj.model.shop.BeanGoodAuctionQuote;
import com.tdkj.model.shop.BeanOrder;
import com.tdkj.model.shop.BeanOrderConsigneeInfo;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.shop.IBeanGoodAuctionQuoteService;
import com.tdkj.service.shop.IBeanGoodAuctionService;
import com.tdkj.service.shop.IBeanOrderService;
import com.tdkj.service.shop.IBeanShoppingCartService;
import com.tdkj.util.PublicConfing;

public class GoodAuctionAction extends BaseAction {

	private List<BeanGoodAuctionQuote> quotes;
	
	private Long goodId ;
	
	private Long id ;
	
	private String state = "1";
	
	private IBeanGoodAuctionService goodAuctionService;
	
	private IBeanGoodAuctionQuoteService goodAuctionQuoteService;
	
	private IBeanShoppingCartService shoppingCartService;
	
	private IBeanOrderService orderService;
	
	private BeanGoodAuction auction;
	
	private BeanOrderConsigneeInfo consigneeInfo;
	
	private Integer logisticsApproach;
	
	private BigDecimal logisticsPrice;
	
	private BeanOrder order ;
	
	private List<BeanOrderConsigneeInfo> consigneeInfos;
	
	public String execute(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		
		String hql = "select distinct quote.auction from " + BeanGoodAuctionQuote.class.getName() + " as quote where quote.user.id='"+userId+"' order by quote.auction.startTime desc";
		Query query = this.createQuery(hql);
		
		this.queryByPage(query, "goodAuctionAction.action", 10);
		
		return "success";
	}
	
	public String auction(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		
		goodAuctionQuoteService.add(goodId, userId);
		
		return execute();
	}
	
	
	public String confirm(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		
		auction = goodAuctionService.findById(id, BeanGoodAuction.class);
		
		if(userId == auction.getFinalQuote().getUser().getId()){
			return "confirm";
		}else{
			return "success";
		}
	}
	
	public String createOrder(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		if(consigneeInfo.getId() == null){
			shoppingCartService.addConsigneeInfo(userId, consigneeInfo);
		}
		List<BeanOrder> orders = orderService.create(id, userId, consigneeInfo, logisticsApproach, logisticsPrice);
		order = orders.get(0);
		return "orderToPay";
	}

	public List<BeanGoodAuctionQuote> getQuotes() {
		return quotes;
	}

	public void setQuotes(List<BeanGoodAuctionQuote> quotes) {
		this.quotes = quotes;
	}

	@Resource
	public void setGoodAuctionQuoteService(
			IBeanGoodAuctionQuoteService goodAuctionQuoteService) {
		this.goodAuctionQuoteService = goodAuctionQuoteService;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public Long getGoodId() {
		return goodId;
	}

	public void setGoodId(Long goodId) {
		this.goodId = goodId;
	}

	@Resource
	public void setGoodAuctionService(IBeanGoodAuctionService goodAuctionService) {
		this.goodAuctionService = goodAuctionService;
	}

	public BeanGoodAuction getAuction() {
		return auction;
	}

	public void setAuction(BeanGoodAuction auction) {
		this.auction = auction;
	}

	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}

	@Resource
	public void setOrderService(IBeanOrderService orderService) {
		this.orderService = orderService;
	}

	public BeanOrderConsigneeInfo getConsigneeInfo() {
		return consigneeInfo;
	}

	public void setConsigneeInfo(BeanOrderConsigneeInfo consigneeInfo) {
		this.consigneeInfo = consigneeInfo;
	}

	public Integer getLogisticsApproach() {
		return logisticsApproach;
	}

	public void setLogisticsApproach(Integer logisticsApproach) {
		this.logisticsApproach = logisticsApproach;
	}

	public BigDecimal getLogisticsPrice() {
		return logisticsPrice;
	}

	public void setLogisticsPrice(BigDecimal logisticsPrice) {
		this.logisticsPrice = logisticsPrice;
	}

	public BeanOrder getOrder() {
		return order;
	}

	public void setOrder(BeanOrder order) {
		this.order = order;
	}

	@Resource
	public void setShoppingCartService(IBeanShoppingCartService shoppingCartService) {
		this.shoppingCartService = shoppingCartService;
	}

	public List<BeanOrderConsigneeInfo> getConsigneeInfos() {
		return consigneeInfos;
	}

	public void setConsigneeInfos(List<BeanOrderConsigneeInfo> consigneeInfos) {
		this.consigneeInfos = consigneeInfos;
	}
	
	
}
